/**
 * 算法：从二叉排序树移除一个节点
 * https://leetcode.cn/problems/delete-node-in-a-bst/
 * 本算法亮点是逻辑清晰，有典型的 bst 代码框架。特别是在处理要删除的节点左、右子树都存在时的分类逻辑非常好：右子树找到最左一个节点，把左子树挂上去。右子树整个代替被删除节点。
 * 
 * 测试数据 (按层遍历-bfs)：
 * [0],0
 * [5,3,6,2,4,null,7],3
 * [5,3,6,2,4,null,7],0
 * 
*/

#include <sstream>
#include <queue>
#include <iostream>
using namespace std;


struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        if (root == nullptr) return nullptr;
        // key 在右子树
        if(key > root->val) {
            root->right = deleteNode(root->right, key);
        }else
        // key 在左子树
        if(key < root->val) root->left = deleteNode(root->left, key);
        //删除当前节点
        else{
            // 左子树为空，右子树顶上
            if(root->left == nullptr){
                return root->right;
            }
            // 右子树为空，左子树顶上
            if(root->right == nullptr){
                return root->left;
            }
            // 左、右子树都存在
            TreeNode* successor = root->right;
            while(successor->left != nullptr){
                successor = successor->left;
            }
            successor->left = root->left;
            root = root->right;
        }
        return root;
    }
};

void trimLeftTrailingSpaces(string &input) {
    input.erase(input.begin(), find_if(input.begin(), input.end(), [](int ch) {
        return !isspace(ch);
    }));
}

void trimRightTrailingSpaces(string &input) {
    input.erase(find_if(input.rbegin(), input.rend(), [](int ch) {
        return !isspace(ch);
    }).base(), input.end());
}

TreeNode* stringToTreeNode(string input) {
    trimLeftTrailingSpaces(input);
    trimRightTrailingSpaces(input);
    input = input.substr(1, input.length() - 2);
    if (!input.size()) {
        return nullptr;
    }

    string item;
    stringstream ss;
    ss.str(input);

    getline(ss, item, ',');
    TreeNode* root = new TreeNode(stoi(item));
    queue<TreeNode*> nodeQueue;
    nodeQueue.push(root);

    while (true) {
        TreeNode* node = nodeQueue.front();
        nodeQueue.pop();

        if (!getline(ss, item, ',')) {
            break;
        }

        trimLeftTrailingSpaces(item);
        if (item != "null") {
            int leftNumber = stoi(item);
            node->left = new TreeNode(leftNumber);
            nodeQueue.push(node->left);
        }

        if (!getline(ss, item, ',')) {
            break;
        }

        trimLeftTrailingSpaces(item);
        if (item != "null") {
            int rightNumber = stoi(item);
            node->right = new TreeNode(rightNumber);
            nodeQueue.push(node->right);
        }
    }
    return root;
}

int stringToInteger(string input) {
    return stoi(input);
}

string treeNodeToString(TreeNode* root) {
    if (root == nullptr) {
      return "[]";
    }

    string output = "";
    queue<TreeNode*> q;
    q.push(root);
    while(!q.empty()) {
        TreeNode* node = q.front();
        q.pop();

        if (node == nullptr) {
          output += "null, ";
          continue;
        }

        output += to_string(node->val) + ", ";
        q.push(node->left);
        q.push(node->right);
    }
    return "[" + output.substr(0, output.length() - 2) + "]";
}

int main() {
    string line;
    while (getline(cin, line)) {
        TreeNode* root = stringToTreeNode(line);
        getline(cin, line);
        int key = stringToInteger(line);
        
        TreeNode* ret = Solution().deleteNode(root, key);

        string out = treeNodeToString(ret);
        cout << out << endl;
    }
    return 0;
}